﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class ChangePasswordUser : Form
    {
        string userName;
        public ChangePasswordUser(string user)
        {
            InitializeComponent();
            userName = user;
            IssuePermits();
        }

        private void IssuePermits()
        {
            //buttonChange.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void ChangePasswordUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void textBoxNewPassword_Validating(object sender, CancelEventArgs e)
        {
            errorProviderChangePassword.SetError(textBoxNewPassword,ValidateFields.PasswordValidation(textBoxNewPassword.Text));
        }

        private void textBoxConfirmPassword_Validating(object sender, CancelEventArgs e)
        {
            errorProviderChangePassword.SetError(textBoxConfirmPassword,ValidateFields.ConfirmPasswordValidation(textBoxConfirmPassword.Text));
        }

        private void buttonChange_Click(object sender, EventArgs e)
        {
            if(ValidateFields.ValidatingFieldsChangePassword()){
                if (UserManager.ValidateConfirmPassword(textBoxNewPassword.Text, textBoxConfirmPassword.Text))
                {
                    if (UserManager.ModifyPasswordUser(userName, textBoxPassword.Text, textBoxNewPassword.Text))
                    {
                        MessageBox.Show("Successful changed password user");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("The password could not be changed"); 
                    }
                }
                else 
                {
                    MessageBox.Show("The passwords you entered do not match.");
                }
            }
            else
            {
                MessageBox.Show("All fields are required");
            }
       }
    }
}
